package com.ict.dataprocess;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ict.config.Config;
import com.ict.utils.CacheObject;
import com.ict.utils.ConnMQUtil;
import com.ict.utils.ConnRedisUtil;
import com.ict.utils.MQTopicPublish;
import com.ict.utils.MessageStruct;
import com.ict.utils.PublishInit;

import team.iOceanPlus.PB.CommonEnum.PB_Enum_TargetInfo_Type;
import team.iOceanPlus.PB.Target;
import team.iOceanPlus.PB.Target.PBTarget;

public class OrigRedisToMQ {
	private static Logger logger = Logger.getLogger(OrigRedisToMQ.class);

	public void run() {
		System.out.println("third start");
		// ConnMQUtil connMQUtil = new ConnMQUtil();
		try {
			Map<byte[], byte[]> rt_map = ConnRedisUtil.jedis.hgetAll("mid_pos_new".getBytes());
			List<Target.PBTargetPosition> lstSendAISPB = new ArrayList<Target.PBTargetPosition>();
			List<Target.PBTargetPosition> lstSendArgosPB = new ArrayList<Target.PBTargetPosition>();
			List<Target.PBTargetPosition> lstSendBeidouPB = new ArrayList<Target.PBTargetPosition>();
			List<Target.PBTargetPosition> lstSendHaiJianPB = new ArrayList<Target.PBTargetPosition>();
			List<Target.PBTargetPosition> lstSendLRITPB = new ArrayList<Target.PBTargetPosition>();

			for (byte[] bytKey : rt_map.keySet()) {

				Target.PBTargetPosition indexPos = (Target.PBTargetPosition) byteArrayToObject(rt_map.get(bytKey));
				if (PB_Enum_TargetInfo_Type.EV_TargetInfoType_AISDynamic_VALUE == indexPos
						.getEnumTargetInfoTypeValue()) {
					lstSendAISPB.add(indexPos);
				}
				if (PB_Enum_TargetInfo_Type.EV_TargetInfoType_ArgosAndMaritimeSatellite_VALUE == indexPos
						.getEnumTargetInfoTypeValue()) {
					lstSendArgosPB.add(indexPos);
				}
				if (PB_Enum_TargetInfo_Type.EV_TargetInfoType_Beidou_VALUE == indexPos.getEnumTargetInfoTypeValue()) {
					lstSendBeidouPB.add(indexPos);
				}
				if (PB_Enum_TargetInfo_Type.EV_TargetInfoType_Haijian_VALUE == indexPos.getEnumTargetInfoTypeValue()) {
					lstSendHaiJianPB.add(indexPos);
				}
				if (PB_Enum_TargetInfo_Type.EV_TargetInfoType_LRIT_VALUE == indexPos.getEnumTargetInfoTypeValue()) {
					lstSendLRITPB.add(indexPos);
				}
			}
			logger.info("orig--AIS  position  count is" + lstSendAISPB.size());
			logger.info("orig--argo position  count is" + lstSendArgosPB.size());
			logger.info("orig--Beidou   position count is" + lstSendBeidouPB.size());
			logger.info("orig--Haijian  position count is" + lstSendHaiJianPB.size());
			logger.info("orig--LRIT     position count is" + lstSendLRITPB.size());
			Target.PBTarget.Builder aisBuilder = Target.PBTarget.newBuilder();
			aisBuilder.setEnumSenderSoftwareValue(6);
			aisBuilder.addAllListTargetPos(lstSendAISPB);
			Target.PBTarget aisTarget = aisBuilder.build();
			// connMQUtil.send("topic", Config.sendExChangeName,
			// "OnLine.PreprocessedData.AIS", null,aisTarget.toByteArray());

			List<MessageStruct> lstMsg = new ArrayList<MessageStruct>();
			MessageStruct aisMsgStruct = new MessageStruct("OnLine.PreprocessedData.AIS", aisTarget.toByteArray());
			lstMsg.add(aisMsgStruct);

			Target.PBTarget.Builder argosBuilder = Target.PBTarget.newBuilder();
			argosBuilder.setEnumSenderSoftwareValue(6);
			argosBuilder.addAllListTargetPos(lstSendArgosPB);
			Target.PBTarget argosTarget = argosBuilder.build();
			// connMQUtil.send("topic", Config.sendExChangeName,
			// "OnLine.PreprocessedData.Argos", null,argosTarget.toByteArray());
			MessageStruct argosMsgStruct = new MessageStruct("OnLine.PreprocessedData.Argos",
					argosTarget.toByteArray());
			lstMsg.add(argosMsgStruct);
			Target.PBTarget.Builder beiDouBuilder = Target.PBTarget.newBuilder();
			beiDouBuilder.setEnumSenderSoftwareValue(6);
			beiDouBuilder.addAllListTargetPos(lstSendBeidouPB);
			Target.PBTarget beiDouTarget = beiDouBuilder.build();
			// connMQUtil.send("topic", Config.sendExChangeName,
			// "OnLine.PreprocessedData.BeiDou", null,beiDouTarget.toByteArray());
			MessageStruct beiDouMsgStruct = new MessageStruct("OnLine.PreprocessedData.BeiDou",
					beiDouTarget.toByteArray());
			lstMsg.add(beiDouMsgStruct);
			Target.PBTarget.Builder haijianBuilder = Target.PBTarget.newBuilder();
			haijianBuilder.setEnumSenderSoftwareValue(6);
			haijianBuilder.addAllListTargetPos(lstSendHaiJianPB);
			Target.PBTarget haijianTarget = haijianBuilder.build();
			// connMQUtil.send("topic", Config.sendExChangeName,
			// "OnLine.PreprocessedData.HaiJian", null,haijianTarget.toByteArray());
			MessageStruct haiJianMsgStruct = new MessageStruct("OnLine.PreprocessedData.HaiJian",
					haijianTarget.toByteArray());
			lstMsg.add(haiJianMsgStruct);
			Target.PBTarget.Builder LRITBuilder = Target.PBTarget.newBuilder();
			LRITBuilder.setEnumSenderSoftwareValue(6);
			LRITBuilder.addAllListTargetPos(lstSendLRITPB);
			Target.PBTarget LRITTarget = LRITBuilder.build();
			// connMQUtil.send("topic", Config.sendExChangeName,
			// "OnLine.PreprocessedData.LRIT", null,LRITTarget.toByteArray());
			MessageStruct LRITMsgStruct = new MessageStruct("OnLine.PreprocessedData.LRIT", LRITTarget.toByteArray());
			lstMsg.add(LRITMsgStruct);
			// connMQUtil.sendClose();
			PublishInit.publish.Publish(lstMsg);
			ConnRedisUtil.jedis.del("mid_pos_new".getBytes());
			logger.info("orig--send success !");
			// ConnRedisUtil.jedis.del("recv_orig_pb");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
	}

	public static Object byteArrayToObject(byte[] bytes) {
		Object obj = null;
		ByteArrayInputStream byteArrayInputStream = null;
		ObjectInputStream objectInputStream = null;
		try {
			byteArrayInputStream = new ByteArrayInputStream(bytes);
			objectInputStream = new ObjectInputStream(byteArrayInputStream);
			obj = objectInputStream.readObject();
		} catch (Exception e) {
		} finally {
			if (byteArrayInputStream != null) {
				try {
					byteArrayInputStream.close();
				} catch (IOException e) {
				}
			}
			if (objectInputStream != null) {
				try {
					objectInputStream.close();
				} catch (IOException e) {
				}
			}
		}
		return obj;
	}
}
